package com.ogilvy.audi.webapp.action;

import java.util.List;

import com.ogilvy.audi.model.CircleTcmnt;
import com.ogilvy.audi.model.CircleTopic;
import com.ogilvy.audi.service.CircleTcmntManager;
import com.ogilvy.audi.util.ServiceDispatcher;
import com.opensymphony.xwork2.Preparable;

public class CircleTcmntAction extends BaseAction implements Preparable {
    private CircleTcmntManager circleTcmntManager;
    private List<CircleTcmnt> circleTcmnts;
    private CircleTcmnt circleTcmnt;
    private Long cmtId;
    private String query;
    private CircleTopic circleTopic;
   
    private String topicId;
    public ServiceDispatcher serviceDispatcher;
    public void setCircleTcmntManager(CircleTcmntManager circleTcmntManager) {
        this.circleTcmntManager = circleTcmntManager;
    }

    public List getCircleTcmnts() {
        return circleTcmnts;
    }

    /**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
        if (getRequest().getMethod().equalsIgnoreCase("post")) {
            // prevent failures on new
            String circleTcmntId = getRequest().getParameter("circleTcmnt.cmtId");
            if (circleTcmntId != null && !circleTcmntId.equals("")) {
                circleTcmnt = circleTcmntManager.get(new Long(circleTcmntId));
            }
        }
    }

    public void setQ(String q) {
        this.query = q;
    }

    public String list() {
    	//circleTopics = circleTopicManager.search(query, CircleTopic.class);
    	circleTcmnts = serviceDispatcher.getCircleTcmntManager().getRecordsByHql("from CircleTcmnt",null);
        //circleTcmnts = circleTcmntManager.search(query, CircleTcmnt.class);
        return "list";
    }

    public void setCmtId(Long cmtId) {
        this.cmtId = cmtId;
    }

    public CircleTcmnt getCircleTcmnt() {
        return circleTcmnt;
    }

    public void setCircleTcmnt(CircleTcmnt circleTcmnt) {
        this.circleTcmnt = circleTcmnt;
    }

    public String delete() {
        circleTcmntManager.remove(circleTcmnt.getCmtId());
        saveMessage(getText("circleTcmnt.deleted"));

        return SUCCESS;
    }

    public String edit() {
        if (cmtId != null) {
            circleTcmnt = circleTcmntManager.get(cmtId);
        } else {
            circleTcmnt = new CircleTcmnt();
        }

        return SUCCESS;
    }

    public String save() throws Exception {
        if (cancel != null) {
            return "cancel";
        }

        if (delete != null) {
            return delete();
        }

        boolean isNew = (circleTcmnt.getCmtId() == null);

        circleTcmntManager.save(circleTcmnt);

        String key = (isNew) ? "circleTcmnt.added" : "circleTcmnt.updated";
        saveMessage(getText(key));

        if (!isNew) {
            return INPUT;
        } else {
            return SUCCESS;
        }
    }
}